package com.zrwl.poscloud.core.restapi.admin.authority;

import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.base.entity.Menus;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.core.config.admin.LocalApiConfig;
import com.zrwl.poscloud.xo.service.MenusService;
import com.zrwl.poscloud.xo.vo.MenusVO;
import com.zrwl.poscloud.xo.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能菜单 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-04-16
 */
@RestController
@Api(value = "功能菜单相关接口", tags = {"功能菜单相关接口"})
@RequestMapping("/menus")
@Slf4j
public class MenusRestApi {

    @Autowired
    private MenusService menusService;
    @Autowired
    LocalApiConfig localApiConfig;

    @AuthorityVerify
    @ApiOperation(value = "获取功能菜单列表", notes = "获取功能菜单列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取功能菜单列表: {}", menusVO);
        return ResultUtil.successWithData(menusService.getPageList(menusVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取功能菜单列表", notes = "获取功能菜单列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取功能菜单列表: {}", menusVO);
        return ResultUtil.successWithData(menusService.getAllList(menusVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增功能菜单")
    @ApiOperation(value = "新增功能菜单", notes = "新增功能菜单", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增功能菜单: {}", menusVO);
        String resultStr = menusService.add(menusVO);
        localApiConfig.changeLocalAndCloudUrl(menusVO);
        return resultStr;
    }

    @AuthorityVerify
    @OperationLogger(value = "修改功能菜单")
    @ApiOperation(value = "修改功能菜单", notes = "修改功能菜单", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改功能菜单: {}", menusVO);
        String resultStr = menusService.edit(menusVO);
        localApiConfig.changeLocalAndCloudUrl(menusVO);
        return resultStr;
    }

    @AuthorityVerify
    @OperationLogger(value = "删除功能菜单")
    @ApiOperation(value = "删除功能菜单", notes = "删除功能菜单", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除功能菜单: {}", menusVO);
        String resultStr = menusService.delete(menusVO);
        localApiConfig.changeLocalAndCloudUrl(menusVO);
        return resultStr;
    }

    @AuthorityVerify
    @OperationLogger(value = "查询所有功能菜单列表")
    @ApiOperation(value = "查询所有功能菜单列表", notes = "查询所有功能菜单列表", response = String.class)
    @PostMapping("/getAllMenuList")
    public String getAllMenuList(@RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("查询所有功能菜单列表: {}", menusVO);
        return ResultUtil.successWithData(menusService.getAllMenuList(menusVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "查询所有页面菜单")
    @ApiOperation(value = "查询所有页面菜单", notes = "查询所有页面菜单", response = String.class)
    @PostMapping("/getAllPageMenuList")
    public String getAllPageMenuList(@RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取功能页面级联下拉框列表: {}", menusVO);
        /***
         * 1.获取所有根级功能页面
         * 2.分别查询功能页面下子功能页面
         * 3.递归执行子功能页面查询
         */
        List<MenusVO> resultList = new ArrayList<>();
        List<Menus> rootList = menusService.getRootMenusList();
        if (!rootList.isEmpty() && rootList != null) {
            for (Menus items : rootList) {
                MenusVO entity = new MenusVO();
                BeanUtils.copyProperties(items, entity);
                resultList.add(entity);
                menusService.getChildrenList(entity);
            }
        }
        return ResultUtil.successWithData(resultList);
    }

    @AuthorityVerify
    @OperationLogger(value = "根功能列表")
    @ApiOperation(value = "根功能列表", notes = "根功能列表", response = String.class)
    @PostMapping("/getRootList")
    public String getRootList(@RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("根功能列表: {}", menusVO);
        return ResultUtil.successWithData(menusService.getRootMenusList());
    }

    @AuthorityVerify
    @OperationLogger(value = "查询功能菜单树")
    @ApiOperation(value = "查询功能菜单树", notes = "查询功能菜单树", response = String.class)
    @PostMapping("/getMenuTreeList")
    public String getMenuTreeList(@RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("查询功能菜单树: {}", menusVO);
        return ResultUtil.successWithData(menusService.getMenuTreeList(menusVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "查询拥有权限功能菜单树")
    @ApiOperation(value = "查询拥有权限功能菜单树", notes = "查询拥有权限功能菜单树", response = String.class)
    @PostMapping("/getOwnMenuTreeList")
    public String getOwnMenuTreeList(@RequestBody RoleVO roleVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("查询拥有权限功能菜单树: {}", roleVO);
        return ResultUtil.successWithData(menusService.getOwnMenuTreeList(roleVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "查询未拥有权限功能菜单树")
    @ApiOperation(value = "查询未拥有权限功能菜单树", notes = "查询未拥有权限功能菜单树", response = String.class)
    @PostMapping("/getNotOwnMenuTreeList")
    public String getNotOwnMenuTreeList(@RequestBody RoleVO roleVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("查询未拥有权限功能菜单树: {}", roleVO);
        return ResultUtil.successWithData(menusService.getNotOwnMenuTreeList(roleVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取功能菜单列表按钮", notes = "获取功能菜单列表按钮", response = String.class)
    @PostMapping("/getAPIMenusList")
    public String getAPIMenusList(@Validated({GetList.class}) @RequestBody MenusVO menusVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取功能菜单列表按钮: {}", menusVO);
        return ResultUtil.successWithData(menusService.getAPIMenusList(menusVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "切换功能按钮访问模式")
    @ApiOperation(value = "切换功能按钮访问模式", notes = "切换功能按钮访问模式", response = String.class)
    @PostMapping("/batchMenusModeTrans")
    public String batchMenusModeTrans(@Validated({Delete.class}) @RequestBody List<MenusVO> menusVOList, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("切换功能按钮访问模式: {}", menusVOList);
        String resultStr = menusService.batchMenusModeTrans(menusVOList);
        localApiConfig.initializeLocalAndCloudUrl();
        return resultStr;
    }

}
